

; SCHIERO
; By Viktor Klasson & Robert Cranston



; Main
; run this file to run Schiero



(load "optimization.scm")
(load "globals.scm")
(load "user-interface.scm")
(load "game-manager.scm")
(load "graphics-object.scm") ; for graphics-cache
;(require profile)



(define (schiero)
  ; Fork to seperate thread to make sure GUI works
  (thread (lambda ()
            ; cache graphics in memory
            (graphics-cache)
            ; Create user-interface
            (let ([user-interface (new user-interface%)])
              ; Set up controller handler
              (set! *user-interface* user-interface)
              ; Create game-manager
              (let ([game-manager (new game-manager%)])
                ; Add game-manager as controller
                (send *user-interface* add-controller game-manager)
                ; Game Loop
                ;(profile-thunk (lambda ()
                (let game-loop ()
                  (unless (or (send user-interface quit?) (send game-manager quit?))
                    ; Debug handling
                    (when *debug* (debug-reset))
                    ; Update
                    (send game-manager update)
                    ; Render
                    (send user-interface render game-manager)
                    ; Try to keep fps
                    ;(sleep (flmax #i0 (- *spf* *time-delta*)))
                    ; Update global time
                    (let ([time-current (current-second)])
                      (unless (eq? *time-updated* #f)
                        (set! *time-delta* (flmax #i1/1000 (- time-current *time-updated*))))
                      (set! *time-updated* time-current))
                    ; Loop
                    (game-loop)))
                ; Remove game manager from list of controllers and delete handler
                (send *user-interface* remove-controller game-manager)
                (set! *user-interface* #f)
                ; Destroy window
                (send user-interface destroy)
                ;))
                ))))
  ; Remove output
  (void))


(schiero)


